﻿import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQuick.Dialogs 1.2
import QtSensors 5.3
import QtQuick.Window 2.2


ApplicationWindow {
    visible: true
    width: Screen.width
    height: Screen.height
    title: qsTr("小球")

    property real screenWidth: Screen.width
    property real screenHeight: Screen.height

    property real ballRadius: 500

    property real ballCenterX: 0
    property real ballCenterY: 0

    property var colors: ["red", "green", "yellow", "blue", "gray"]

    property int score: 0

    property bool isGameOver: false

    property int maxScore: 0


    MainForm{
        id: mainForm
        anchors.fill: parent
        bubble{
            id: bubble
            centerX: mainWindow.width/2
            centerY: mainWindow.height/2
            bubbleCenter: bubble.width/2
            x: bubble.centerX - bubble.bubbleCenter
            y: bubble.centerY - bubble.bubbleCenter

            Behavior on y {
                SmoothedAnimation {
                    easing.type: Easing.Linear
                    duration: 100
                }
            }
            Behavior on x {
                SmoothedAnimation {
                    easing.type: Easing.Linear
                    duration: 100
                }
            }
        }
    }

    Accelerometer{
        id: accel
        dataRate: 100
        active: true

        onReadingChanged: {
            if(isGameOver) return
            var newX = (mainForm.bubble.x + calcRoll(accel.reading.x, accel.reading.y, accel.reading.z) * 2)
            var newY = (mainForm.bubble.y - calcPitch(accel.reading.x, accel.reading.y, accel.reading.z) * 2)

            if (isNaN(newX) || isNaN(newY))
                return;

            if (newX < 0)
                newX = 0

            if (newX > mainForm.mainWindow.width - mainForm.bubble.width)
                newX = mainForm.mainWindow.width - mainForm.bubble.width

            if (newY < 18)
                newY = 18

            if (newY > mainForm.mainWindow.height - mainForm.bubble.height)
                newY = mainForm.mainWindow.height - mainForm.bubble.height

            mainForm.bubble.x = newX
            mainForm.bubble.y = newY
        }
    }


    Rectangle{
        id: ballRect
        width: ballRadius*2
        height: ballRadius*2
        scale: 0
        radius: ballRadius

        transform: Scale{
            origin.x: ballRect.width/2
            origin.y: ballRect.height/2
        }

    }

//    Button{
//        id: btnRestart
//        anchors.centerIn: parent
//        visible: isGameOver
//        text: "重新开始"
//        z: 10
//        onClicked: {
//            ballRect.scale = 0
//            isGameOver = false
//            timerGame.restart()
//            timer.restart()
//        }
//    }
    Image{
        id: btnRestart
        anchors.centerIn: parent
        visible: isGameOver
        source: "qrc:/btn.png"
        z: 10
        Text {
            id: name
            anchors.centerIn: parent
            text: qsTr("重新开始")
        }

        MouseArea{
            anchors.fill: parent
            onPressed: {
                ballRect.scale = 0
                isGameOver = false
                timerGame.restart()
                timer.restart()
            }
        }
    }

    Text{
        id: scoreText
        text: ("分数: ") + String(score) + "  最高分：" + String(maxScore)
        font.pixelSize: 50
        anchors.right: parent.right
        anchors.rightMargin: 10
        y: 10
    }

    Text{
        id: log
        text: "("+ballCenterX+","+ballCenterY+")"
        font.pixelSize: 50
        font.bold: true
        anchors.bottom: parent.bottom
        visible: false
    }

    ParallelAnimation{
        id: animOpen
        NumberAnimation{target: ballRect; property: "scale"; from: 0; to: 1; duration: 1500}

        onStopped: {
            if(isGameOver) return
            ballRect.scale = 0
            score++
        }
    }

    ParallelAnimation{
        id: animClose
        NumberAnimation{target: ballRect; property: "scale"; from: 1; to: 0; duration: 1500}
    }

    //生成球
    Timer{
        id: timer
        interval: 2000
        repeat: true
        running: true
        onTriggered: {

            var s = getSeed()
            Math.seed = s

            ballCenterX = Math.floor(Math.random()*(screenWidth-ballRadius)) + ballRadius/2
            ballCenterY = Math.floor(Math.random()*(screenHeight-ballRadius)) + ballRadius/2

            var index = Math.floor(Math.random()*5)

            ballRect.x = ballCenterX - ballRadius
            ballRect.y = ballCenterY - ballRadius
            ballRect.color = colors[index]
            animOpen.restart()

        }
    }

    Timer{
        id: timerGame
        interval: 30
        repeat: true
        running: true
        onTriggered: {
            if(funcIsGameOver()){
                isGameOver = true
                timerGame.stop()
                timer.stop()
                maxScore = Math.max(maxScore, score)
                score = 0
                animOpen.stop()
            }
        }
    }

    function funcIsGameOver(){
        //检测是否被球碰到，死亡
//        ballCenterX,ballCenterY
        var bx = mainForm.bubble.x + mainForm.bubble.bubbleCenter
        var by = mainForm.bubble.y + mainForm.bubble.bubbleCenter
        var br = mainForm.bubble.bubbleCenter

        var dt = Math.sqrt(Math.abs(ballCenterX-bx)*Math.abs(ballCenterX-bx) + Math.abs(ballCenterY-by)*Math.abs(ballCenterY-by))

        if(dt < ballRadius*ballRect.scale + br ){
            return true
        }

        return false
    }

    function calcPitch(x, y, z) {
        return -(Math.atan(y / Math.sqrt(x * x + z * z)) * 57.2957795);
    }
    function calcRoll(x, y, z) {
        return -(Math.atan(x / Math.sqrt(y * y + z * z)) * 57.2957795);
    }

    function addZero(value){
        if(value < 10){
            return "0" + value
        }
        return value
    }

    function getSeed(){

        var date = new Date
        var y = date.getFullYear()
        var M = addZero(date.getMonth())
        var d = addZero(date.getDate())
        var h = addZero(date.getHours())
        var m = addZero(date.getMinutes())
        var s = addZero(date.getSeconds())

        var time = String(y) + String(M) + String(d) + String(h) + String(m) + String(s)

        return Number(time)
    }

}

